/*
* 1videoConference -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, Adiance Technologies Pvt. Ltd.
*
* Hardik Sanghvi <hardik.sanghvi @adiance.com>
*
* See http://www.1videoconference.org for more information about
* the 1videoConference project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Xml;
using System.Reflection;
using System.IO;
using Adiance.VMukti.BAL;
using System.Diagnostics;

namespace InterfaceFiles
{
   public class svcInterfaceFiles
    {
        public static XmlDocument xmldoc = new XmlDocument();
        public static string[] SerAddress = null;
        public static string serviceAddress;        

        public static Assembly a;
        public static Assembly b = Assembly.GetAssembly(typeof(svcInterfaceFiles));
        public static ArrayList alAllFiles = new ArrayList();

        public svcInterfaceFiles()
        {
            try
            {
                if (!Directory.Exists(b.Location.Replace("Adiance.VMukti.BAL.dll", @"Zip Files")))
                {
                    Directory.CreateDirectory(b.Location.Replace("Adiance.VMukti.BAL.dll", @"Zip Files"));
                }
                if (!Directory.Exists(b.Location.Replace("Adiance.VMukti.BAL.dll", @"Business Layer")))
                {
                    Directory.CreateDirectory(b.Location.Replace("Adiance.VMukti.BAL.dll", @"Business Layer"));
                }

                string interfacefolder = b.Location.Replace("Adiance.VMukti.BAL.dll", @"Business Layer");
                DirectoryInfo interfaceinfo = new DirectoryInfo(interfacefolder);
                ShowDirectory(interfaceinfo);

                if (!Directory.Exists(b.Location.Replace("Adiance.VMukti.BAL.dll", @"Presentation Layer")))
                {
                    Directory.CreateDirectory(b.Location.Replace("Adiance.VMukti.BAL.dll", @"Presentation Layer"));
                }
                string controlfolder = b.Location.Replace("Adiance.VMukti.BAL.dll", @"Presentation Layer");
                DirectoryInfo dirinfocontrol = new DirectoryInfo(controlfolder);
                ShowAllFiles(dirinfocontrol);


                if (!Directory.Exists(b.Location.Replace("Adiance.VMukti.BAL.dll", @"Data Layer")))
                {
                    Directory.CreateDirectory(b.Location.Replace("Adiance.VMukti.BAL.dll", @"Data Layer"));
                }
                string dalfolder = b.Location.Replace("Adiance.VMukti.BAL.dll", @"Data Layer");
                DirectoryInfo dirinfodal = new DirectoryInfo(dalfolder);
                ShowAllFiles(dirinfodal);

                string balfolder = b.Location.Replace("Adiance.VMukti.BAL.dll", @"Business Layer");
                DirectoryInfo dirinfobal = new DirectoryInfo(balfolder);
                ShowAllFiles(dirinfobal);
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "Adiance.VMukti.BAL--:--svcInterfaceFiles.cs--:--svcInterfaceFiles()--:--");
                Adiance.VMukti.BAL.clsUtility.LogException = exp;
                //System.Windows.MessageBox.Show("svcInterfaceFiles" + exp.Message);
            }
        }

        public static void ShowDirectory(DirectoryInfo dir)
        {
            try
            {
                foreach (FileInfo file in dir.GetFiles("*.xml"))
                {
                    UserService usrService = new UserService();
                    usrService.XmlFileName = file.FullName;
                    xmldoc.Load(file.FullName);
                    AddNode(xmldoc.DocumentElement, ref usrService);
                    UserServices.MyServices.Add(usrService);

                    EventLog entLog2 = new EventLog("Application", Environment.MachineName, "Adiance");
                    entLog2.WriteEntry("Control Name: " + usrService.ControlName);
                    entLog2.Close();

                }
                foreach (DirectoryInfo subDir in dir.GetDirectories())
                {
                    ShowDirectory(subDir);
                }
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "Adiance.VMukti.BAL--:--svcInterfaceFiles.cs--:--ShowDirectory()--:--");
                Adiance.VMukti.BAL.clsUtility.LogException = exp;
                //System.Windows.MessageBox.Show("ShowDirectory" + exp.Message);
            }

            //for (int i = 0; i < UserServices.MyServices.Count; i++)
            //{
            //    EventLog entLog2 = new EventLog("Application", Environment.MachineName, "Adiance");
            //    entLog2.WriteEntry("UserServices.MyServices[" + i.ToString() + "] = " + UserServices.MyServices[i].ServiceName[0]);
            //    entLog2.Close();
            //}

        }

        public static void ShowAllFiles(DirectoryInfo di)
        {
            try
            {
                foreach (FileInfo file in di.GetFiles("*.dll"))
                {
                    int hj = alAllFiles.Add(file.FullName);
                }
                foreach (DirectoryInfo subDir in di.GetDirectories())
                {
                    ShowAllFiles(subDir);
                }
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "Adiance.VMukti.BAL--:--svcInterfaceFiles.cs--:--ShowAllFiles()--:--");
                Adiance.VMukti.BAL.clsUtility.LogException = exp;
                //System.Windows.MessageBox.Show("ShowAllFiles"+exp.Message);
            }
        }

        private static void AddNode(XmlNode inXmlNode, ref UserService parUsrService)
        {
            try
            {
                XmlNode xNode;
                XmlNodeList nodeList;
                int i;
                if (inXmlNode.Attributes[0].Name == "control")
                {
                    parUsrService.ControlName = inXmlNode.Attributes[0].Value.ToString();
                }
                if (inXmlNode.HasChildNodes)
                {

                    nodeList = inXmlNode.ChildNodes;
                    for (i = 0; i <= nodeList.Count - 1; i++)
                    {
                        xNode = inXmlNode.ChildNodes[i];
                        if (xNode.Attributes.Count > 1)
                        {
                            for (int j = 0; j < xNode.Attributes.Count; j++)
                            {
                                if (xNode.Attributes[j].Name.ToLower() == "name")
                                {
                                    parUsrService.ServiceName.Add(xNode.Attributes[j].Value.ToString());
                                    EventLog entLog2 = new EventLog("Application", Environment.MachineName, "Adiance");
                                    entLog2.WriteEntry("Service Name: " + xNode.Attributes[j].Value.ToString());
                                    entLog2.Close();
                                }
                                if (xNode.Attributes[j].Name.ToLower() == "binding")
                                {
                                    parUsrService.ServiceBinding.Add(xNode.Attributes[j].Value.ToString());                                    

                                }
                                if (xNode.Attributes[j].Name.ToLower() == "bindingtype")
                                {
                                    parUsrService.BindingType.Add(xNode.Attributes[j].Value.ToString());
                                }
                            }
                        }
                        AddNode(xNode, ref parUsrService);
                    }
                }

                if (inXmlNode.Name.ToLower() == "object")
                {
                    try
                    {
                        for (i = 0; i < inXmlNode.Attributes.Count; i++)
                        {
                            switch (inXmlNode.Attributes.Item(i).Name.ToLower())
                            {
                                case "classname":
                                    parUsrService.ClassName.Add(inXmlNode.Attributes.GetNamedItem(inXmlNode.Attributes.Item(i).Name).Value.ToString());
                                    //parUsrService.ServerInterface.Add(inXmlNode.Attributes.GetNamedItem(inXmlNode.Attributes.Item(i).Name).Value.ToString());
                                    break;
                                case "parametrized":
                                    parUsrService.Parameters.Add(inXmlNode.Attributes.GetNamedItem(inXmlNode.Attributes.Item(i).Name).Value.ToString());
                                    break;

                            }
                        }
                    }
                    catch (Exception exp)
                    {
                        exp.Data.Add("My Key", "Adiance.VMukti.BAL--:--svcInterfaceFiles.cs--:--AddNode()1--:--");
                        Adiance.VMukti.BAL.clsUtility.LogException = exp;
                        //System.Windows.MessageBox.Show("AddNode" + exp.Message);
                    }
                }
                else if (inXmlNode.Name.ToLower() == "serverinterface")
                {
                    try
                    {
                        for (i = 0; i < inXmlNode.Attributes.Count; i++)
                        {
                            switch (inXmlNode.Attributes.Item(i).Name.ToLower())
                            {
                                case "value":                                    
                                    parUsrService.ServerInterface.Add(inXmlNode.Attributes.GetNamedItem(inXmlNode.Attributes.Item(i).Name).Value.ToString());
                                    break;
                               
                            }
                        }
                    }
                    catch (Exception exp)
                    {
                        exp.Data.Add("My Key", "Adiance.VMukti.BAL--:--svcInterfaceFiles.cs--:--AddNode()2--:--");
                        Adiance.VMukti.BAL.clsUtility.LogException = exp;
                       // System.Windows.MessageBox.Show("AddNode"+exp.Message);
                    }
                }
                else if (inXmlNode.Name.ToLower() == "clientinterface")
                {
                    try
                    {
                        for (i = 0; i < inXmlNode.Attributes.Count; i++)
                        {
                            switch (inXmlNode.Attributes.Item(i).Name.ToLower())
                            {
                                case "value":
                                    parUsrService.ClientInterface.Add(inXmlNode.Attributes.GetNamedItem(inXmlNode.Attributes.Item(i).Name).Value.ToString());
                                    break;
                            }
                        }
                    }
                    catch (Exception exp)
                    {
                        exp.Data.Add("My Key", "Adiance.VMukti.BAL--:--svcInterfaceFiles.cs--:--AddNode()3--:--");
                        Adiance.VMukti.BAL.clsUtility.LogException = exp;
                        //System.Windows.MessageBox.Show(""+exp.Message);
                    }
                }
                else if (inXmlNode.Name.ToLower() == "serverclass")
                {
                    try
                    {
                        for (i = 0; i < inXmlNode.Attributes.Count; i++)
                        {
                            switch (inXmlNode.Attributes.Item(i).Name.ToLower())
                            {
                                case "value":
                                    parUsrService.ServerClass.Add(inXmlNode.Attributes.GetNamedItem(inXmlNode.Attributes.Item(i).Name).Value.ToString());
                                    break;
                            }
                        }
                    }
                    catch (Exception exp)
                    {
                        exp.Data.Add("My Key", "Adiance.VMukti.BAL--:--svcInterfaceFiles.cs--:--AddNode()4--:--");
                        Adiance.VMukti.BAL.clsUtility.LogException = exp;
                        //System.Windows.MessageBox.Show("AddNode" + exp.Message);
                    }
                }
            }
            catch (Exception exp1)
            {
                exp1.Data.Add("My Key", "Adiance.VMukti.BAL--:--svcInterfaceFiles.cs--:--AddNode()5--:--");
                Adiance.VMukti.BAL.clsUtility.LogException = exp1;
                //System.Windows.MessageBox.Show("AddNode" + exp1.Message);
            }
        }
    }
}
